﻿function MoveParm(srcX, srcY, status, movePojo) {
	this.srcX = srcX;
	this.srcY = srcY;
	this.status = status;
	this.movePojo = movePojo;
}
function OpenUrl(title, url, width, height, objName, type,callFunction) {
	this.title = title;
	this.url = url;
	this.width = width;
	this.height = height;
	this.innerWidth = parseInt(width) - 12;
	this.innerHeight = parseInt(height) - 26 - 6;
	this.type = type;
	this.moveParm = null;
	this.left = 200;
	this.top = 200;
	this.objName = objName;
	this.closeCallFunction=callFunction;
	this.getWinLeft();
	this.getWinTop();

	this.objDiv = null;
	this.maskDiv = null;// 模板的div
	this.moveDiv = null;// 要生成的div
	this.iframeName=this.objName+"win_dialog_iframe";
}
OpenUrl.prototype = {
	mouseDown : function(ev, cty) {
		if (null == this.moveDiv) {
			this.moveDiv = document.createElement("DIV");
			this.moveDiv.style.width = this.width + "px";
			this.moveDiv.style.height = this.height + "px";
			this.moveDiv.style.position = "absolute";
			document.body.appendChild(this.moveDiv);
			this.moveDiv.className = "moveDiv";
			JBF.addEvent(this.maskDiv, "mousemove", this.moveDivMth(this));
			JBF.addEvent(this.maskDiv, "mouseup", this.mouseUp(this));
		}
		this.moveDiv.style.left = this.objDiv.style.left;
		this.moveDiv.style.top = this.objDiv.style.top;
		var x = JBF.getX(ev);
		var y = JBF.getY(ev);
		var left = this.objDiv.style.left.replace("px", "");
		var top = this.objDiv.style.top.replace("px", "");
		this.moveParm = new MoveParm(x - parseInt(left), y - parseInt(top),
				true, null);
		this.maskDiv.style.zIndex="100000";
	},
	moveDivMth : function(op) {
		return function() {
			if (op.moveParm, op.moveParm.status) {
				var ev = JBF.getEvent();
				if (null != op.moveDiv) {
					op.moveDiv.style.left = (JBF.getX(ev) - op.moveParm.srcX)+ "px";
					op.moveDiv.style.top = (JBF.getY(ev) - op.moveParm.srcY)
							+ "px";
				}
			}
		};
	},
	mouseUp : function(op) {
		return function() {
			op.objDiv.style.left = op.moveDiv.style.left;
			op.objDiv.style.top = op.moveDiv.style.top;
			op.moveDiv.style.display = "none";
			op.moveParm.status = false;
			op.maskDiv.style.zIndex="990";
		};
	},
	getWinLeft : function() {
		this.left = (JBF.getScreenWidth() - parseInt(this.width)) / 2;
		if(this.left<0){
			this.left=0;
		}
	},
	getWinTop : function() {
		this.top = (JBF.getScreenHeight() - parseInt(this.height)) / 2 ;
		if (this.top < 0) {
			this.top = 0;
		}
	},
	close : function() {
		if (this.objDiv) {
			this.objDiv.innerHTML = "";
			this.objDiv.style.display = "none";
		}
		if (this.maskDiv) {
			this.maskDiv.style.display = "none";
		}
		if (this.moveDiv) {
			this.moveDiv.style.display = "none";
		}
		newDialog = null;
		if(this.closeCallFunction){
			this.closeCallFunction();
		}
	},
	closeOver : function(cty) {
		if (cty.className) {
			 cty.className == "title_close"?cty.className = "title_close_over":cty.className = "title_close";
		}
	},
	show : function() {
		if (null == this.objDiv) {
			this.objDiv = document.createElement("DIV");
			this.objDiv.style.position = "absolute";
			document.body.appendChild(this.objDiv);
		}
		if (null == this.maskDiv) {
			this.maskDiv = document.createElement("DIV");
			this.maskDiv.className = "maskDiv";
			this.maskDiv.style.width = JBF.getScreenWidth() + "px";
			this.maskDiv.style.height =JBF.getScreenHeight() + "px";
			document.body.appendChild(this.maskDiv);
		}
		var srcStr = "!|content!|";
		var endStr = "操作错误";
		this.objDiv.style.width = this.width;
		this.objDiv.style.height = this.height;
		this.objDiv.style.left = this.left + "px";
		this.objDiv.style.top = this.top + "px";
		this.objDiv.style.display = "inline";
		this.objDiv.style.zIndex = 1001;
		
		this.maskDiv.style.display = "inline";
		var str = "<div class='title' style='width:"
				+ this.width
				+ "px;cursor: move;'   onmousedown='"
				+ this.objName
				+ ".mouseDown(event,this);'> <div class='title_left'></div> <div class='title_center' style='width:"
				+ this.innerWidth
				+ "px;'><div class='title_ico'></div><div class='title_text'>"
				+ this.title
				+ "</div><div class='title_close' onmousedown='window.event.cancelBubble=true;' onmouseout='"
				+ this.objName
				+ ".closeOver(this);' onmouseover='"
				+ this.objName
				+ ".closeOver(this);' onclick='"
				+ this.objName
				+ ".close();'></div></div> <div class='title_right'></div> </div> <div style='width:"
				+ this.width
				+ "px;height:"
				+ this.innerHeight
				+ "px;'><div class='content_left'></div> <div class='content' style='width:"
				+ this.innerWidth
				+ "px;'> "
				+ srcStr
				+ " </div><div class='content_right'></div> </div> <div style='width:"
				+ this.width
				+ "px; height:6px;'> <div class='footer_left'></div> <div class='footer_center' style='width:"
				+ this.innerWidth
				+ "px;'></div> <div class='footer_right'></div> </div>";
		if (this.type == "url") {
			endStr = "<iframe id='"+this.iframeName+"' name='"+this.iframeName+"' src='" + this.url
					+ "'  width='100%' height='100%' z-index:-1;></iframe>";
		}else if(this.type=="content"){
			endStr = "<div class='   logo_"
					+ this.type
					+ "'></div><div class='show_text'>"
					+ this.url
					+ "</div>";
		}else{
			endStr = this.url;
				 
		}
		str = str.replace(srcStr, endStr);
		this.objDiv.innerHTML = str;
		if(this.type == "url"){
			JBF.el(this.iframeName).src=JBF.el(this.iframeName).src;
		}
	}
};
Dialog = (function() {
	return {
		open : function(obj) {
			var title = obj.title ? obj.title : "对话框";
			var url = obj.url ? obj.url : "";
			var width = obj.width ? obj.width : "300";
			var height = obj.height ? obj.height : "150";
			if(null==newDialog){
				newDialog = new OpenUrl(title, url, width, height, 'newDialog',
				"url");
				newDialog.show();
			}
		},
		success : function(text) {
			newDialog = new OpenUrl("提示", text, "300", "150", 'newDialog',
					"success");
			newDialog.show();
		},
		error : function(text) {
			newDialog = new OpenUrl("错误", text, "300", "150", 'newDialog',
					"error");
			newDialog.show();
		},
		warn : function(text) {
			newDialog = new OpenUrl("警告", text, "300", "150", 'newDialog',
					"warn");
			newDialog.show();
		}
	};
})();